#include "VWindowSplitter.h"
#include <QDebug>
#include <QHBoxLayout>
#include <QSplitter>
#include <QDateTime>

VWindowSplitter::VWindowSplitter(QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout *pLayout = new QHBoxLayout(this);
    pLayout->setContentsMargins(0,0,0,0);
}

void VWindowSplitter::addWidget(QWidget *dst, QWidget *src, Qt::Orientation orientation)
{
    if(!src)
        return ;

    if (nullptr == dst)
    {
        if(0 == this->layout()->count())
        {
            QSplitter *pSplitter = new QSplitter(this);
            pSplitter->addWidget(src);
            this->layout()->addWidget(pSplitter);
        }
    }
    else
    {
        QSplitter *pPSplitter = qobject_cast<QSplitter *>(dst->parentWidget());

        if (!pPSplitter)
            return ;

        pPSplitter->setOrientation(orientation);

        QSplitter *pSplitterDst = new QSplitter(this);
        pSplitterDst->addWidget(dst);

        QSplitter *pSplitterSrc = new QSplitter(this);
        pSplitterSrc->addWidget(src);

        pPSplitter->addWidget(pSplitterDst);
        pPSplitter->addWidget(pSplitterSrc);
    }
}

void VWindowSplitter::removeWidget(QWidget *src)
{
    qDebug()<<"11";
    if(!src || !src->parentWidget())
        return ;

    QSplitter *pPPSplitter = qobject_cast<QSplitter *>(src->parentWidget()->parentWidget());
    if (nullptr == pPPSplitter)
    {
        qDebug()<<"11";
        if(qobject_cast<QSplitter *>(src->parentWidget()))
            delete src->parentWidget(); //src & splitter
    }
    else
    {
        qDebug()<<"11";
        QSplitter *pUSplitter = (QSplitter *)pPPSplitter->widget(!pPPSplitter->indexOf(src->parentWidget())); //QSplitter只会装两个 index非0即1

        QSplitter *pPPPSplitter = qobject_cast<QSplitter *>(pPPSplitter->parentWidget());
        if (nullptr == pPPPSplitter) {
            this->layout()->addWidget(pUSplitter);
        } else {
            pPPPSplitter->insertWidget(pPPPSplitter->indexOf(pPPSplitter),pUSplitter);
        }

        delete pPPSplitter; //删除src 删除pSplitter
    }
    qDebug()<<"11";
}

VSession *VWindowSplitter::selectSession()
{
    return m_pSelected;
}

VSession *VWindowSplitter::vSplitScreen(QString strId)
{
    VSession *pSession = newSession(this,strId);
    addWidget(m_pSelected,pSession,Qt::Vertical);
    selectedSession(pSession);

    return pSession;
}

VSession *VWindowSplitter::hSplitScreen(QString strId)
{
    VSession *pSession = newSession(this,strId);
    addWidget(m_pSelected,pSession,Qt::Horizontal);
    selectedSession(pSession);

    return pSession;
}

void VWindowSplitter::slotMergeScreen()
{
    qDebug()<<"999999";
    removeWidget(m_pSelected);
    qDebug()<<"101111111";
}

VSession *VWindowSplitter::newSession(QWidget *parent,QString strId)
{
    //有会话了
    if(m_listVSession.isEmpty()){
        emit sigNoSession(false);
    }

    VSession *pSession = new VSession(strId,parent);

    //会话被选中
    connect(pSession,&VSession::sigFormSelected,[=]{
        if(pSession->isChecked())
            return ;

        selectedSession(pSession);
    });

    //会话被销毁
    connect(pSession,&VSession::destroyed,[=]{
        m_listVSession.removeOne(pSession);
        emit sigSessionClose(pSession->id());
        if(pSession == m_pSelected)
        {
            m_pSelected = nullptr;

            //自动选中最后一个会话
            if(!m_listVSession.isEmpty()){
                selectedSession(m_listVSession.last());
            }
        }

        //无会话
        if(m_listVSession.isEmpty()){
            emit sigNoSession(true);
        }
    });

    m_listVSession.append(pSession);
    return pSession;
}

void VWindowSplitter::selectedSession(VSession *pSession)
{
    if(m_pSelected)
        m_pSelected->setFormChecked(false); //取消上个选中的会话
    pSession->setFormChecked(true); //选中当前会话
    m_pSelected = pSession;
    QString strSessionId = pSession->id();
    qInfo()<<"选中的会话Id："<<strSessionId;
    emit sigSelectSession(strSessionId);
}
